package com.zwt.util;

import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class TTime {

    public static String fullFormat = "yyyy-MM-dd HH:mm:ss";

    public static String FULL_SIMPLE_FORMAT = "yyyyMMddHHmmss";
    /**
     * 标准日期类型
     */
    public static String dateFormat = "yyyy-MM-dd";
    /**
     * 简单日期类型
     */
    public final static String SIMPLE_DATEFORMAT = "yyyyMMdd";
    public final static String SIMPLE_DATEFORMAT1 = "yyyy.MM.dd";
    public final static String SIMPLE_DATEFORMAT2 = "yyyy.MM.dd HH:mm:ss";
    public final static String SIMPLE_DATEFORMAT3 = "yyyy/MM/dd HH:mm:ss";
    public final static String SIMPLE_DATEFORMAT4 = "yyyy/MM/dd";
    public final static String SIMPLE_DATEFORMAT5 = "MM月dd日";
    public final static String SIMPLE_DATEFORMAT6 = "MM月dd日 HH:mm";




    /**
     * 一天
     */
    public final static Integer ONE_DAY = 24 * 60 * 60;


    /**
     * 推送时间段
     */
    public final static String TS_START = "10:00";
    public final static String TS_END = "22:00";


    public final static int TEN_MINUTE = 10 * 60;

    public static int getTime() {
        return (int) (System.currentTimeMillis() / 1000);
    }

    public static Date getDate() {
        return new Date();
    }


    public static Date getlastDate() {
        Calendar cal = getCalandar();
        cal.add(Calendar.DATE, -1);
        return cal.getTime();
    }


    /**
     * 获取上一年
     * @return
     */
    public static String getLastYear(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.YEAR, -1);
        Date y = c.getTime();
        String year = format.format(y);
        return year;
    }



    /**
     * @param @param  time
     * @param @return 设定文件
     * @return int    返回类型
     * @throws
     * @Title: getValid
     * @Description: TODO(时间大于当天的截止时间在赋值为当前日期的截止时间)
     */
    public int getValid(int time) {
        int cTime = getTime();
        String data = TTime.toDate(cTime, "yyyy-MM-dd") + " 23:59:59";
        int end = TTime.toTime(data, "yyyy-MM-dd HH:mm:ss");
        if (time > end) {
            return end;
        } else {
            return time;
        }
    }

    public static Calendar getCalandar(String date, String fromat) {
        Date thisDate = format(date, fromat);
        return getCalandar(thisDate);
    }

    public static Calendar getCalandar() {
        return getCalandar(null);
    }

    public static Calendar getCalandar(Date date) {
        Calendar cd = Calendar.getInstance();
        if (date != null) {
            cd.setTime(date);
        }
        return cd;
    }

    public static Date format(String date, String fromat) {
        SimpleDateFormat sdf = new SimpleDateFormat(fromat);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }


    public static Date formatExt(String date, String fromat) throws Exception {
        if (date == null || date.trim().length() == 0) {
            return null;
        }
        if (StringUtils.isEmpty(fromat)) {
            fromat = dateFormat;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(fromat);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            throw e;
        }
    }

    public static String format(String fromat, int seconds) {
        Calendar cd = Calendar.getInstance();
        cd.setTimeInMillis((long) seconds * 1000);
        SimpleDateFormat sdf = new SimpleDateFormat(fromat);
        return sdf.format(cd.getTime());
    }

    /**
     * 获取当前时间前的某几个小时
     * @param num  小时值
     * @return
     */
    public static String beforeHourToNowDate(Integer num) {
        Calendar calendar = Calendar.getInstance();
        /* HOUR_OF_DAY 指示一天中的小时 */
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - num);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = df.format(calendar.getTime());
        return format;
    }

    /**
     *  获取当前时间x天后的时间
     * @param x
     * @param dateFormat  时间格式
     * @return
     */
    public static String afterNowDate(Integer x,String dateFormat){
        SimpleDateFormat s = new SimpleDateFormat(dateFormat);
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, x);//计算x天后的时间
        String date =s.format(c.getTime());
        return date;
    }



    public static int toTime(String source, String format) {
        if (format == null || source == null) {
            return -1;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = simpleDateFormat.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (date != null) {
            return (int) (date.getTime() / 1000);
        }
        return -1;
    }

    public static String toDate(int source, String format) {
        String nullKey = "null";
        String s = String.valueOf(source);
        if (s.isEmpty() || nullKey.equals(s)) {
            return "";
        }
        if (org.apache.commons.lang3.StringUtils.isEmpty(format)) {
            format = fullFormat;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(s + "000")));
    }

    /**
     * 判断时间是否在某个时间段内
     * 如果在返回true 否则返回false
     * @param now
     * @return
     */
    public static boolean belongCalendar(Date now) {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
        Date beginTime = null;
        Date endTime = null;
        try {
            now = df.parse(df.format(new Date()));
            beginTime = df.parse(TS_START);
            endTime = df.parse(TS_END);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Calendar date = Calendar.getInstance();
        date.setTime(now);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    public static int diffDate(long starttime) {
        return (int) (System.currentTimeMillis() / 1000 - starttime) / 86400;
    }

    public static int diffDate(int start, int end) {
        return (end - start) / 86400;
    }

    public static Date addTimeForMinute(Date date, int value) {
        return addTime(date, Calendar.MINUTE, value);
    }

    public static Date addTimeForHour(Date date, int value) {
        return addTime(date, Calendar.HOUR, value);
    }

    public static Date addTimeForDay(Date date, int value) {
        return addTime(date, Calendar.DATE, value);
    }

    public static Date addTimeForMonth(Date date, int value) {
        return addTime(date, Calendar.MONTH, value);
    }

    public static Date addTimeForWeek(Date date, int value) {
        return addTime(date, Calendar.WEEK_OF_MONTH, value);
    }

    public static Date addTime(Date date, int field, int value) {
        if (date == null) {
            return null;
        }
        Calendar cal = getCalandar(date);
        cal.add(field, value);
        return cal.getTime();
    }

    /**
     * 获取日期的具体值，比如，获取日期的年、天、日、时、分、秒等信息。
     *
     * @param date  日期
     * @param field 获取字段，Calendar.YEAR 定义的内容等。
     * @return 返回具体的数值
     */
    public static int getNumberFromDate(Date date, int field) {
        Calendar cal = getCalandar(date);
        return cal.get(field);
    }

    /**
     * 获取日期的‘年’。
     *
     * @param date 日期
     * @return 返回具体的数值
     */
    public static int getYear(Date date) {
        return getNumberFromDate(date, Calendar.YEAR);
    }

    /**
     * 获取日期的‘月’。
     *
     * @param date 日期
     * @return 返回具体的数值
     */
    public static int getMonth(Date date) {
        return getNumberFromDate(date, Calendar.MONTH) + 1;
    }

    /**
     * 获取日期的‘日’。
     *
     * @param date 日期
     * @return 返回具体的数值
     */
    public static int getDayOfMonth(Date date) {
        return getNumberFromDate(date, Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取日期的小时
     *
     * @param date
     * @return
     */
    public static int getHourOfDay(Date date) {
        return getNumberFromDate(date, Calendar.HOUR_OF_DAY);
    }

    /**
     * 获取日期的分钟
     *
     * @param date
     * @return
     */
    public static int getMinuteOfDay(Date date) {
        return getNumberFromDate(date, Calendar.MINUTE);
    }

    /**
     * 指定该date时间的小时、分钟和秒的信息。
     *
     * @param date   指定日期时间
     * @param hour
     * @param minute
     * @param second
     * @return
     */
    public static Date setTime(Date date, int hour, int minute, int second) {
        return setTime(date, hour, minute, second, -1);
    }

    public static Date setTime(Date date, int hour, int minute, int second,
                               int millSecond) {
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        if (hour > -1) {
            cal.set(Calendar.HOUR_OF_DAY, hour);
        }
        if (minute > -1) {
            cal.set(Calendar.MINUTE, minute);
        }
        if (second > -1) {
            cal.set(Calendar.SECOND, second);
        }
        if (millSecond > -1) {
            cal.set(Calendar.MILLISECOND, millSecond);
        }
        return cal.getTime();
    }

    public static String parseStringFromDate(Date date, String format) {
        return parseStringFromDate(date, format, Locale.US);
    }

    public static String parseStringFromDate(Date date, String format,
                                             Locale locale) {
        return parseStringFromDate(date, format, locale, null);
    }

    public static String parseStringFromDate(Date date, String format,
                                             Locale locale, TimeZone tz) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat fm = new SimpleDateFormat(format, locale);
        if (tz != null) {
            fm.setTimeZone(tz);
        }
        return fm.format(date);
    }

    /**
     * @param @param  month
     * @param @return 设定文件
     * @return int    返回类型
     * @throws
     * @Title: getDaysByYearAndMonth
     * @Description: TODO(根据某年 ， 某月获取当月有几天)
     */
    public static int getDaysByYearAndMonth(int year, int month) {
        Calendar time = Calendar.getInstance();
        time.clear();
        time.set(Calendar.YEAR, year);
        //year年
        time.set(Calendar.MONTH, month - 1);
        //Calendar对象默认一月为0,month月 //本月份的天数
        return time.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * @param @param  time
     * @param @return 设定文件
     * @return int    返回类型
     * @throws
     * @Title: getYear
     * @Description: TODO(获取年的格式 # - 01 - 01 00 : 00 : 00)
     */
    public static int getYear(int time) {
        Date date = new Date(Long.valueOf(time + "000"));
        int year = getYear(date);
        String years = year + "-01-01 00:00:00";
        year = toTime(years, fullFormat);
        return year;
    }

    /**
     * @param @param  time
     * @param @return 设定文件
     * @return int    返回类型
     * @throws
     * @Title: getMonth
     * @Description: TODO(获取月的格式 2015 - # - 01 00 : 00 : 00的时间戳)
     */
    public static int getMonth(int time) {
        Date date = new Date(Long.valueOf(time + "000"));
        int year = getYear(date);
        int month = getMonth(date);
        String months = String.valueOf(year) + "-" + String.valueOf(month) + "-01 00:00:00";
        month = toTime(months, "yyyy-MM-dd HH:mm:ss");
        return month;
    }

    /**
     * @param @param  time
     * @param @return 设定文件
     * @return int    返回类型
     * @throws
     * @Title: getDay
     * @Description: TODO(获取日的格式 2015 - 01 - # 00 : 00 : 00的时间戳)
     */
    public static int getDay(int time) {
        Date date = new Date(Long.valueOf(time + "000"));
        int year = getYear(date);
        int month = getMonth(date);
        int day = getDayOfMonth(date);
        String days = String.valueOf(year) + "-" + String.valueOf(month) + "-" + String.valueOf(day) + " 00:00:00";
        day = toTime(days, "yyyy-MM-dd HH:mm:ss");
        return day;
    }

    /**
     * @param @param  time
     * @param @return 设定文件
     * @return int    返回类型
     * @throws
     * @Title: getDay
     * @Description: TODO(获取日的格式 2015 - 01 - # 00 : 00 : 00的时间戳)
     */
    public static int getHoursOfDay(int time) {
        Date date = new Date(Long.valueOf(time + "000"));
        int year = getYear(date);
        int month = getMonth(date);
        int day = getDayOfMonth(date);
        int hour = getHourOfDay(date);
        String days = String.valueOf(year) + "-" + String.valueOf(month) + "-" + String.valueOf(day) + " " + String.valueOf(hour) + ":00:00";
        day = toTime(days, "yyyy-MM-dd HH:mm:ss");
        return day;
    }

    public static void main(String[] args) {
//		int year = getYear(1446188776);
//        System.out.println(TTime.addTimeForDay(new Date(), (-365 * (36 + 1))));
//		System.out.println(parseStringFromDate(addTimeForDay(new Date(), (-365*11)),"yyyy-MM-dd"));
//		String data = TTime.toDate(cTime, "yyyy-MM-dd")+" 23:59:59";
//		int end = TTime.toTime(data, "yyyy-MM-dd HH:mm:ss");
        Date date1 = TTime.format("2017-11-28 12:03:01", fullFormat);
        Date date2 = TTime.format("2017-11-26 12:01:01", fullFormat);

        int i = differentDays(date2, date1);
        System.out.println(i);
    }

    /**
     * 相差的年数
     *
     * @param date1
     * @param date2
     * @return
     * @throws ParseException
     */
    public static int getYearSpace(Date date1, Date date2) {
        int result;
        Calendar c1 = getCalandar();
        Calendar c2 = getCalandar();
        c1.setTime(date1);
        c2.setTime(date2);
        result = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
        return result == 0 ? 1 : Math.abs(result);

    }

    static String[] constellationArr = {"水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座"};
    static int[] constellationEdgeDay = {20, 19, 21, 20, 21, 22, 23, 23, 23, 24, 23, 22};

    public static String getConstellation(Integer month, Integer day) {
        if (null == month || null == day || 0 == day) {
            return null;
        }
        month = month - 1;
        if (day < constellationEdgeDay[month]) {
            month = month - 1;
        }
        if (month >= 0) {
            return constellationArr[month];
        }
        // default to return 魔羯
        return constellationArr[11];
    }


    /**
     * 清空日期的时分秒
     *
     * @param cl
     * @return
     */
    public static Date cleanHMS(Calendar cl) {
        if (cl == null) {
            return null;
        }
        cl.set(Calendar.HOUR_OF_DAY, 0);
        cl.set(Calendar.MINUTE, 0);
        cl.set(Calendar.SECOND, 0);
        cl.set(Calendar.MILLISECOND, 0);
        return cl.getTime();
    }

    /**
     * date2比date1多的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        //同一年
        if (year1 != year2) {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                //闰年
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
                    timeDistance += 366;
                    //不是闰年
                } else {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2 - day1);
            //不同年
        } else {
            return day2 - day1;
        }
    }

    /**
     * 判断今天是星期几
     * 1 : 周日
     * 2 : 周一
     * .
     * .
     * .
     * 7 : 周六
     *
     * @param today
     * @return
     */
    public static int getWeekDay(Date today) {
        Calendar c = getCalandar(today);
        return c.get(Calendar.DAY_OF_WEEK);
    }

    /**
     * 获取指定时间
     * @param thisDate new Date("2018/04/24 23:59:59")
     * @param days -1 前一天  +1 后一天
     * @param hour 小时
     * @param minute 分钟
     * @param second 秒钟
     * @return
     */
    public static Date getAssignDate(Date thisDate,int days,int hour,int minute,int second){
        return TTime.setTime(TTime.addTimeForDay(thisDate, days)
                ,hour,minute,second);
    }

    public static final int daysBetween(Date early, Date late) {

        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(early);
        c2.setTime(late);

        return daysBetween(c1, c2);
    }

    public static final int daysBetween(Calendar early, Calendar late) {

        return (int) (toJulian(late) - toJulian(early));
    }

    public static final float toJulian(Calendar c) {

        int Y = c.get(Calendar.YEAR);
        int M = c.get(Calendar.MONTH);
        int D = c.get(Calendar.DATE);
        int A = Y / 100;
        int B = A / 4;
        int C = 2 - A + B;
        float E = (int) (365.25f * (Y + 4716));
        float F = (int) (30.6001f * (M + 1));
        float JD = C + D + E + F - 1524.5f;

        return JD;
    }


}
